package name.wilbur.course.db.bigone.ui;

import java.util.Iterator;
import java.util.Map;

import name.wilbur.course.db.bigone.control.AdminMgr;
import name.wilbur.course.db.bigone.model.City;
import name.wilbur.course.db.bigone.model.User;
import name.wilbur.course.db.bigone.ui.custom.ListContentProvider;
import name.wilbur.course.db.bigone.ui.custom.ListLabelProvider;
import name.wilbur.course.db.bigone.ui.custom.ListModel;
import name.wilbur.course.db.bigone.util.Checker;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class AdminWin extends BaseWindow {
	private AdminMgr mgr;
	private ListViewer userList, cityList;
	private Group userManageGroup, cityManageGroup, controlGroup;
	private User currUser;
	
	public AdminWin(Map<String, Object> session) {
		super();
		mgr = new AdminMgr(session);
	}
	public AdminWin(Display disp, Map<String, Object> session) {
		super(disp);
		mgr = new AdminMgr(session);
	}

	public void createSShell() {
		final GridData oneSpanGridData = new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1);
		final GridData twoSpanGridData = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
		GridData gridData;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 8;
		gridLayout.makeColumnsEqualWidth = true;
		
		sShell = new Shell();
		sShell.setText("管理员主窗口");
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(500, 300));
		
		gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		// user manage group
		userManageGroup = new Group(sShell, SWT.None);
		userManageGroup.setText("用户列表");
		userManageGroup.setLayout(gridLayout);
		userManageGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 3, 6));
		// user list
		userList = new ListViewer(userManageGroup, SWT.SINGLE | SWT.V_SCROLL);
		userList.setContentProvider(new ListContentProvider<User>(userList));
		userList.setLabelProvider(new ListLabelProvider());
		userList.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 5));
		userList.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				Iterator it = selection.iterator();
				if (it.hasNext())
					currUser = (User)it.next();
//				System.out.println(currFriend);
			}
		});
		// delete user button
		Button deleteUserB = new Button(userManageGroup, SWT.None);
		deleteUserB.setLayoutData(oneSpanGridData);
		deleteUserB.setText("删除用户");
		deleteUserB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				MessageBox warnBox = new MessageBox(sShell, SWT.YES|SWT.NO);
				warnBox.setText("确认删除操作");
				warnBox.setMessage("确认要删除用户" + currUser.getName() + "吗?");
				int result = warnBox.open();
				if (result == SWT.YES) {
					userList.remove(currUser);
					mgr.deleteFriend(currUser);
				}
			}
		});
		// view user info button
		Button viewUserB = new Button(userManageGroup, SWT.None);
		viewUserB.setLayoutData(oneSpanGridData);
		viewUserB.setText("查看用户");
		viewUserB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				UserInfoWin fw = new UserInfoWin(disp, currUser, mgr.getSession());
				fw.open();
			}
		});
		
		gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		// city manage group
		cityManageGroup = new Group(sShell, SWT.None);
		cityManageGroup.setText("城市管理");
		cityManageGroup.setLayout(gridLayout);
		cityManageGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 3, 6));
		// city list
		cityList = new ListViewer(cityManageGroup, SWT.SINGLE | SWT.V_SCROLL);
		cityList.setContentProvider(new ListContentProvider<User>(userList));
		cityList.setLabelProvider(new ListLabelProvider());
		cityList.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 5));
		// import city button
		Button importCityListB = new Button(cityManageGroup, SWT.None);
		importCityListB.setLayoutData(oneSpanGridData);
		importCityListB.setText("导入列表...");
		importCityListB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				FileDialog dialog = new FileDialog(sShell, SWT.OPEN);
				 dialog.setFilterNames(new String[] { "Text Files", "All Files (*.*)" });
				 dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
				 dialog.setFilterPath(".");
				 String filepath = dialog.open();
				 if (Checker.notEmptyString(filepath))
					 cityList.setInput(mgr.importCityListFile(filepath));
			}
		});
		// clear city list button
		Button clearCityListB = new Button(cityManageGroup, SWT.None);
		clearCityListB.setLayoutData(oneSpanGridData);
		clearCityListB.setText("清空列表");
		clearCityListB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				cityList.setInput(mgr.clearCityList());
			}
		});
		
		gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		// control group
		controlGroup = new Group(sShell, SWT.None);
		controlGroup.setText("控制");
		controlGroup.setLayout(gridLayout);
		controlGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 2));
		// apply button
		Button applyB = new Button(controlGroup, SWT.None);
		applyB.setLayoutData(twoSpanGridData);
		applyB.setText("应用更改");
		applyB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				mgr.applyChange();
			}
		});
		// exit button
		Button exitB = new Button(controlGroup, SWT.None);
		exitB.setLayoutData(twoSpanGridData);
		exitB.setText("退出");
		exitB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				close();
			}
		});
	}
	
	public void initWindowData() {
		ListModel<User> usersModel = mgr.getUserList();
		if ((usersModel.getContent() != null) && (usersModel.getContent().get(0) != null))
			userList.setInput(usersModel);
		ListModel<City> citysModel = mgr.getCityList();
		if ((citysModel.getContent() != null) && (citysModel.getContent().get(0) != null))
			cityList.setInput(mgr.getCityList());
	}
}
